#!/usr/bin/env python
# encoding: utf-8
"""
calibration_pattern.py

"""
import numpy as np
from OpenGL.GL import *

class CalibrationPattern():
	"""	variable size calibration grid
		grid is centered on screen center
	"""
	def __init__(self, size=4, scale=200, step=1):
		self.scale = scale
		self.size = size*self.scale
		self.step = step*self.scale
		
	def _make_grid(self):
		self.grid = np.mgrid[0:self.size+self.step:self.step,
		 				0:self.size+self.step:self.step]
		#return self.grid
		
	def _make_pts(self):
		self.pts = [zip(self.grid[0][i], self.grid[1][i]) for i in range(len(self.grid[0]))]
		#return self.pts
		
	def make_rects(self):
		self._make_grid()
		self._make_pts()
		self.rects = [zip(self.pts[i][:-1], self.pts[i+1][1:]) for i in range(len(self.pts)-1)]
		return self.rects
		
	def update_rects(self):
		#if we wanted the calibration rectangle to move
		pass
	
	def get_size(self):
		return self.size
		
	def draw_chessboard(self, grid, grid_size, SCREEN_SIZE):
		glOrtho(0, SCREEN_SIZE[0], SCREEN_SIZE[1], 0, -1, 1)
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()
		glPushMatrix()
		glColor3f(0.0,0.0,0.0)
		glTranslatef(SCREEN_SIZE[0]/2-grid_size/2, SCREEN_SIZE[1]/2-grid_size/2, 0.0)
		for col in grid:
			#glRectfv(col[0][0], col[0][1])
			#glRectfv(col[1][0], col[1][1])
			#glRectfv(col[2][0], col[2][1])
			#glRectfv(col[3][0], col[3][1])
			for r in col:
				#odd columns color different than even
				if grid.index(col)%2 > 0:
					if col.index(r)%2 > 0:
						glColor3f(1.0, 1.0, 1.0)
					else:
						glColor3f(0.0, 0.0, 0.0)
				else:
					if col.index(r)%2 > 0:
						glColor3f(0.0, 0.0, 0.0)
					else:
						glColor3f(1.0, 1.0, 1.0)

				glRectfv(r[0], r[1])
		glPopMatrix()

